package cn.gcyl.module.biz.controller.admin.carouselimage;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.framework.excel.core.util.ExcelUtils;
import cn.gcyl.module.biz.controller.admin.carouselimage.vo.*;
import cn.gcyl.module.biz.dal.dataobject.carouselimage.CarouselImageDO;
import cn.gcyl.module.biz.service.carouselimage.CarouselImageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 轮播图")
@RestController
@RequestMapping("/biz/carousel-image")
@Validated
public class CarouselImageController {

    @Resource
    private CarouselImageService carouselImageService;

    @PostMapping("/create")
    @Operation(summary = "创建轮播图")
//    @PreAuthorize("@ss.hasPermission('biz:carousel-image:create')")
    public CommonResult<Long> createCarouselImage(@Valid @RequestBody CarouselImageSaveReqVO createReqVO) {
        return success(carouselImageService.createCarouselImage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新轮播图")
//    @PreAuthorize("@ss.hasPermission('biz:carousel-image:update')")
    public CommonResult<Boolean> updateCarouselImage(@Valid @RequestBody CarouselImageSaveReqVO updateReqVO) {
        carouselImageService.updateCarouselImage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除轮播图")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:carousel-image:delete')")
    public CommonResult<Boolean> deleteCarouselImage(@RequestParam("id") Long id) {
        carouselImageService.deleteCarouselImage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除轮播图")
//                @PreAuthorize("@ss.hasPermission('biz:carousel-image:delete')")
    public CommonResult<Boolean> deleteCarouselImageList(@RequestParam("ids") List<Long> ids) {
        carouselImageService.deleteCarouselImageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得轮播图")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('biz:carousel-image:query')")
    public CommonResult<CarouselImageRespVO> getCarouselImage(@RequestParam("id") Long id) {
        CarouselImageDO carouselImage = carouselImageService.getCarouselImage(id);
        return success(BeanUtils.toBean(carouselImage, CarouselImageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得轮播图分页")
//    @PreAuthorize("@ss.hasPermission('biz:carousel-image:query')")
    public CommonResult<PageResult<CarouselImageRespVO>> getCarouselImagePage(@Valid CarouselImagePageReqVO pageReqVO) {
        PageResult<CarouselImageDO> pageResult = carouselImageService.getCarouselImagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CarouselImageRespVO.class));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出轮播图 Excel")
////    @PreAuthorize("@ss.hasPermission('biz:carousel-image:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportCarouselImageExcel(@Valid CarouselImagePageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<CarouselImageDO> list = carouselImageService.getCarouselImagePage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "轮播图.xls", "数据", CarouselImageRespVO.class,
//                        BeanUtils.toBean(list, CarouselImageRespVO.class));
//    }

}